package deom1;

class ListNode {
   int val;  
  ListNode next = null;
  public ListNode(int n){
      this.val = n;
  }
}


public class test12 {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        ListNode head1 = new ListNode(-1);
        head1.next = head;
        ListNode cur = head;
        ListNode pre = head1;
        //找到反转的区间
        for (int i = 1; i < m; i++) {
            pre = cur;
            cur = cur.next;
        }
        for (int i = m; i < n; i++) {
            ListNode tmp = cur.next;
            cur.next = tmp.next;
            tmp.next = pre.next;
            pre.next  = tmp;
        }
        return  head1.next;
    }
}